package com.huike.web.controller.clues;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.service.impl.TbClueTrackRecordServiceImpl;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 线索跟进记录Controller
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {

    @Autowired
    private TbClueTrackRecordServiceImpl tbClueTrackRecordService;

    /**
     * 查询线索跟进记录列表
     * @param clueId
     * @return TableDataInfo封装类
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId) {
        String username = SecurityUtils.getUsername();  //当前登录/操作用户的名称
//        Date now = DateUtils.getNowDate();
        LocalDateTime now = LocalDateTime.now();  //现在的时间 用来填充updateTime字段

        TableDataInfo tableDataInfo = new TableDataInfo();
        List<Map<String,Object>> rowsList = tbClueTrackRecordService.listClueTrackRecord(clueId);
        for (Map<String, Object> map : rowsList) {
            map.put("updateBy",username);
            map.put("updateTime",now);
        }
        //给返回值填充字段 完善返回值信息
        tableDataInfo.setRows(rowsList);
        tableDataInfo.setTotal(1);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功！");

       return tableDataInfo;
    }


    /**
     * 新增线索跟进记录
     * @param tbClueTrackRecord
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVo tbClueTrackRecord) {

        tbClueTrackRecordService.addClueTrackRecord(tbClueTrackRecord);
        return AjaxResult.success("操作成功");
    }
}
